package com.liy.framework.common.office.pdf;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import java.io.File;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OfficeToPDF {
    public OfficeToPDF() {
    }

    public static void main(String[] args) {
        officeToPDF("C:\\Temp\\doc\\2.xls", "C:\\Temp\\doc\\3.pdf");
    }

    public static boolean officeToPDF(String sourceFile, String destFile) {
        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return false;
            }

            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }

            if (outputFile.exists()) {
                outputFile.delete();
            }

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("10.10.10.129", 8100);
            connection.connect();
            System.out.println("连接时间:" + df.format(new Date()));
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
            System.out.println("转换时间:" + df.format(new Date()));
            connection.disconnect();
            return true;
        } catch (ConnectException var7) {
            var7.printStackTrace();
            System.err.println("openOffice连接失败！请检查IP,端口");
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return false;
    }
}
